package vno.fux.res.pool
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;

	public class AutoBitmap extends BitmapTarget implements IResoureceDistributor
	{
		private var _recievers:Dictionary;
		public function AutoBitmap()
		{
			super();
		}
		
		override public function saveData(data:Object):void
		{
			_bitmapData = data["bitmapData"];
			if(_recievers)
			{
				refreshChildren(_bitmapData);
			}
		}
		
		private function refreshChildren(value:BitmapData):void
		{
			for each(var child:IResourceAttacher in _recievers)
			{
				child.recieve(value);
			}
		}
		public function attach(reciever:IResourceAttacher):void
		{
			if(!_recievers)
			{
				_recievers = new Dictionary(true);
			}
			if(super._bitmapData)
			{
				reciever.recieve(super._bitmapData)
			}
			_recievers[reciever] = reciever;
		}
		
		public function detach(reciever:IResourceAttacher):Boolean
		{
			return delete _recievers[reciever];
		}
		
		public function detachAll():void
		{
			_recievers = null;
		}
		public function dispose():void
		{
			if(!_bitmapData) return;
			_bitmapData.dispose();
			_bitmapData = null;
		}
	}
}